package com.dxm.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    private static final Pattern pattern = Pattern.compile("A-Z");

    public static boolean isEmpty(String str) {

        return (str == null || "".equals(str));
    }

    public static boolean isNotEmpty(String str) {

        return !isEmpty(str);
    }

    public static boolean isNull(String str) {

        return (str == null || "".equals(str) || "null".equals(str.toLowerCase()));
    }

    public static boolean isNotNull(String str) {

        return !isNull(str);
    }


    //实体类属性转表字段（驼峰式）
    public static String field2Column(String key) {
        Matcher matcher = pattern.matcher(key);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    //region 表字段转实体属性（驼峰命名规范）
    public static String column2Field(String column) {
        StringBuilder sb = new StringBuilder();
        String[] arr = column.split("_");
        for (int i = 0; i < arr.length; i++) {
            if (i == 0) {
                sb.append(arr[i]);
            } else {
                sb.append(String.valueOf(arr[i].charAt(0)).toUpperCase()).append(arr[i].substring(1, arr[i].length()));
            }
        }
        return sb.toString();
    }
    //endregion


    public static void main(String[] args) {
        String a = null;
        String b = "";
        String c = "null";

        System.out.println("isEmpty：" + isEmpty(a));
        System.out.println("isEmpty：" + isEmpty(b));
        System.out.println("isEmpty：" + isEmpty(c));

        System.out.println("isNull：" + isNull(a));
        System.out.println("isNull：" + isNull(b));
        System.out.println("isNull：" + isNull(c));

    }

}
